// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Gas Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.centerPos.y
private alias object.value2 : object.collisionOffset.y
private alias object.value3 : object.launchPos.y
private alias object.value4 : object.flameActive

// States
private alias 0 : GASPLATFORM_AWAITINTERVAL
private alias 1 : GASPLATFORM_POPPED
private alias 2 : GASPLATFORM_AWAITCONTACT
private alias 3 : GASPLATFORM_CONTACT_LAUNCHED
private alias 4 : GASPLATFORM_CONTACT_IDLE

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animationSpeed : player.animationSpeed
private alias object.animation : player.animation

private alias object.value1 : player.timer
private alias object.value5 : player.rollAnimationSpeed


// ========================
// Function Declarations
// ========================

reserve function GasPlatform_DebugDraw
reserve function GasPlatform_DebugSpawn


// ========================
// Function Definitions
// ========================

private function GasPlatform_DebugDraw
	DrawSprite(7)
end function


private function GasPlatform_DebugSpawn
	CreateTempObject(TypeName[Gas Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.y = object.ypos
end function


// ========================
// Events
// ========================

event ObjectUpdate
	temp6 = object.ypos
	switch object.state
	case GASPLATFORM_AWAITINTERVAL
		object.timer++
		if object.timer == 120
			object.timer = 0
			object.yvel = -0x96800
			PlaySfx(SfxName[Gas Pop], false)
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE
			end if
			object.state++
		end if
		break

	case GASPLATFORM_POPPED
		object.ypos += object.yvel
		object.yvel += 0x3800

		if object.ypos >= object.centerPos.y
			object.ypos = object.centerPos.y
			if object.yvel < 0x10000
				object.state = GASPLATFORM_AWAITINTERVAL
				object.yvel  = 0
			else
				object.yvel >>= 2
				FlipSign(object.yvel)
			end if
		end if
		break

	case GASPLATFORM_AWAITCONTACT
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		break

	case GASPLATFORM_CONTACT_LAUNCHED
		object.ypos += object.yvel
		object.yvel += 0x3800

		if object.ypos <= object.launchPos.y
			object.ypos = object.launchPos.y
			object.state++
			PlaySfx(SfxName[Spring], false)
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -10, 24, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					player[currentPlayer].xpos 					= object.xpos
					player[currentPlayer].state 				= Player_State_Static
					player[currentPlayer].gravity 				= 1
					player[currentPlayer].timer 				= 0
					player[currentPlayer].speed 				= 0
					player[currentPlayer].xvel 					= 0
					player[currentPlayer].yvel 					= -0x100000
					player[currentPlayer].animation 			= ANI_JUMPING
					player[currentPlayer].animationSpeed 		= 0xF0
					player[currentPlayer].rollAnimationSpeed	= 0xF0
				end if
			next
		end if
		break

	case GASPLATFORM_CONTACT_IDLE
		break
		
	end switch

	temp0 = object.centerPos.y
	temp0 -= object.ypos
	if temp0 > 0x180000
		object.flameActive = true
	else
		object.flameActive = false
	end if

	temp0 = object.ypos
	temp6 &= 0xFFFF0000
	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp6

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		object.ypos = object.centerPos.y
		BoxCollisionTest(C_SOLID, object.entityPos, -32, 8, 32, 48, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, 0, 16, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if object.flameActive == true
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_FireHit)
			end if
		end if

		if object.yvel < -0x60000
			temp7 = player[currentPlayer].yvel
			temp7 -= 0x20000
		else
			temp7 = -0x80000
		end if

		if object.yvel < temp7
			object.ypos = temp0
			temp7 = player[currentPlayer].yvel
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -10, 24, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				player[currentPlayer].yvel = 0x10000
			end if

			BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -8, 24, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			default
				break
				
			case 4
				if player[currentPlayer].gravity == 0
					if object.yvel > 0
						CallFunction(Player_Kill)
					end if
				end if
				break
			end switch
			player[currentPlayer].yvel = temp7
		else
			object.ypos = temp6
			BoxCollisionTest(C_SOLID2, object.entityPos, -24, -8, 24, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_NONE
				BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -8, 24, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				break

			case COL_TOP
				player[currentPlayer].ypos += object.collisionOffset.y
				if object.state == GASPLATFORM_AWAITCONTACT
					player[currentPlayer].speed = 0
					player[currentPlayer].xvel = 0
					object.yvel = -0x96800
					PlaySfx(SfxName[Gas Pop], false)
					object.state++
				end if
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					if object.yvel > 0
						CallFunction(Player_Kill)
					end if
				end if
				break
			end switch
		end if

		object.ypos = object.centerPos.y
		BoxCollisionTest(C_SOLID, object.entityPos, -16, 0, 16, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
	next
	object.ypos = temp0

	// Animate
	object.frame = object.animationTimer
	object.frame /= 3
	object.animationTimer++
	object.animationTimer %= 21

	if object.outOfBounds == true
		temp0 = object.ypos
		object.ypos = object.centerPos.y
		if object.outOfBounds == true
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
				object.timer 			= 0
				object.animationTimer 	= 0
				object.flameActive 		= false
				object.xvel 			= 0
				object.yvel 			= 0
				if object.propertyValue == 1
					object.state = GASPLATFORM_AWAITCONTACT
				else
					object.state = GASPLATFORM_AWAITINTERVAL
				end if
			end if
		else
			object.ypos = temp0
		end if
	end if
end event


event ObjectDraw
	if object.flameActive == true
		DrawSpriteXY(object.frame, object.xpos, object.centerPos.y)
	end if
	DrawSprite(7)
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-15, -32, 30, 32, 102, 158)
	SpriteFrame(-24, -8, 48, 12, 84, 108)

	foreach (TypeName[Gas Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state  = GASPLATFORM_AWAITCONTACT
			object[arrayPos0].launchPos.y = object[arrayPos0].ypos
			object[arrayPos0].launchPos.y -= 0x780000
		end if
	next

	SetTableValue(TypeName[Gas Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(GasPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(GasPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // type
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			editor.variableValue &= 1
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(7)
	
	// TODO: it'd be cool to show range, right?
end event


event RSDKLoad
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-14, -15, 28, 15, 104, 191)
	SpriteFrame(-15, -24, 30, 24, 102, 133)
	SpriteFrame(-15, -32, 30, 32, 102, 158)
	SpriteFrame(-24, -8, 48, 12, 84, 108)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Pop On Interval", 0)
	AddEnumVariable("Pop On Contact", 1)
end event
